//
//  UIAlertView+Blocks.m
//  CommonLib
//
//  Created by dev on 13-6-3.
//  Copyright (c) 2013年 boxfishedu. All rights reserved.
//

#import "UIAlertView+Blocks.h"

static DismissWithTextFieldBlock _dismissWithTextFieldBlock;
static DismissBlock _dismissBlock;
static CancelBlock _cancelBlock;

@implementation UIAlertView (Blocks)

+ (UIAlertView*)showAlertViewWithTitle:(NSString*)title
                               message:(NSString*)message
                     cancelButtonTitle:(NSString*)cancelButtonTitle
                     otherButtonTitles:(NSArray*)otherButtons
                             onDismiss:(DismissBlock)dismissed
                              onCancel:(CancelBlock)cancelled
{
    _cancelBlock = [cancelled copy];
    _dismissBlock = [dismissed copy];
    _dismissWithTextFieldBlock = nil;
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
    
    for (NSString *buttonTitle in otherButtons) {
        [alert addButtonWithTitle:buttonTitle];
    }
    
    [alert show];
    return alert;

}

+ (UIAlertView*)showAlertViewWithTitle:(NSString*)title
                               message:(NSString*)message
                     cancelButtonTitle:(NSString*)cancelButtonTitle
                     otherButtonTitles:(NSArray*)otherButtons
                onDismissWithTextField:(DismissWithTextFieldBlock)dismissed
                              onCancel:(CancelBlock)cancelled
{
    return [self showAlertViewWithTitle:title message:message cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtons defaultText:nil onDismissWithTextField:dismissed onCancel:cancelled];
}

+ (UIAlertView*)showAlertViewWithTitle:(NSString*)title
                               message:(NSString*)message
                     cancelButtonTitle:(NSString*)cancelButtonTitle
                     otherButtonTitles:(NSArray*)otherButtons
                           defaultText:(NSString*)defaultText
                onDismissWithTextField:(DismissWithTextFieldBlock)dismissed
                              onCancel:(CancelBlock)cancelled
{
    _cancelBlock = [cancelled copy];
    _dismissWithTextFieldBlock = [dismissed copy];
    _dismissBlock = nil;
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    
    for (NSString *buttonTitle in otherButtons) {
        [alert addButtonWithTitle:buttonTitle];
    }
    
    if (defaultText) {
        UITextField *textField = [alert textFieldAtIndex:0];
        textField.text = defaultText;
    }
    
    [alert show];
    return alert;
}


+ (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex  // after animation
{
    if (buttonIndex == [alertView cancelButtonIndex]) {
        if (_cancelBlock) {
            _cancelBlock();
        }
    } else {
        if (_dismissBlock) {
            _dismissBlock(buttonIndex - 1);            
        } else if (_dismissWithTextFieldBlock) {
            _dismissWithTextFieldBlock(buttonIndex - 1, [alertView textFieldAtIndex:0]);
        }
    }
}

@end
